package com.qidian.buy.search.service.impl;

import com.qidian.buy.search.config.RestTemplateConfig;
import com.qidian.buy.search.dao.SearchItemDAO;
import com.qidian.buy.search.pojo.SearchItem;
import com.qidian.buy.search.service.SearchItemService;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.*;

/**
 * Author: 戴着假发的程序员
 * Date: 2024-05-13 12:13
 * company: 起点编程
 **/
@Service
public class SearchItemServiceImpl implements SearchItemService {
    @Autowired
    private SearchItemDAO searchItemDAO;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${GOOD_BASE_URL}")
    private String goodBaseUrl;
    @Override
    public QdResult importIndex(Long itemId) {
        // 远程调用
        QdResult result = restTemplate.getForObject(goodBaseUrl + "/item/get?itemId=" + itemId, QdResult.class);
        if (result.getCode() == 200) {
            // 获取商品信息
            LinkedHashMap data = (LinkedHashMap) result.getData();
            // 转换为SearchItem
            SearchItem item = new SearchItem();
            item.setItemId(new Long(data.get("itemId").toString()));
            item.setItemTitle(data.get("itemTitle").toString());
            item.setItemPrice(new Long(data.get("itemPrice").toString()));
            item.setBuyCount(100);
            item.setAcclaim(9999L);
            // 图片处理，goods提供的接口中可以级联查询商品的轮播图，我们只需要取出其中一个即可
            List<String> images = (List<String>) data.get("images");
            if (images != null && images.size() > 0) {
                item.setImage(images.get(0));
            }
            // 调用DAO进行保存
            int i = searchItemDAO.saveSearchItem(item);
            return QdResult.isSuccess(i);
        }
        return QdResult.error();
    }

    @Override
    public QdResult importsIndex() {
        // 远程调用goods中分页查询商品信息的接口
        int limit = 1000;
        QdResult result = restTemplate.getForObject(goodBaseUrl + "/item/list?page=1&limit=" + limit, QdResult.class);
        if (result.getCode() == 200) {
            // 获取商品信息
            LinkedHashMap iPage = (LinkedHashMap) result.getData();
            List<LinkedHashMap> dataList = (List<LinkedHashMap>) iPage.get("records");
            // 转换为SearchItem
            for (LinkedHashMap data : dataList) {
                SearchItem item = new SearchItem();
                item.setItemId(new Long(data.get("itemId").toString()));
                item.setItemTitle(data.get("itemTitle").toString());
                item.setItemPrice(new Long(data.get("itemPrice").toString()));
                item.setBuyCount(100);
                item.setAcclaim(9999L);
                // 图片处理，goods提供的接口中可以级联查询商品的轮播图，我们只需要取出其中一个即可
                List<String> images = (List<String>) data.get("images");
                if (images != null && images.size() > 0) {
                    item.setImage(images.get(0));
                }
                // 调用DAO进行保存
                int i = searchItemDAO.saveSearchItem(item);
            }
        }
        return QdResult.success();
    }

    @Override
    public QdResult doSearch(int page, int limit, String kw) {
        Map data = new HashMap();
        data.put("page",page);
        data.put("limit",limit);
        data.put("kw",kw);
        data.put("records",searchItemDAO.selectByPage(page,limit,kw));
        data.put("total",searchItemDAO.selectCount(kw));
        data.put("maxPageNum",(int)Math.ceil(new Double(data.get("total").toString()) / limit));
        return QdResult.success(data);
    }
}
